﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MemoLib.Tasks;
using MemoLib.Entity;

namespace MemoLib
{
    public class Utility
    {
        public static string GenerateID()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmss");
        }

        public static CycleTaskCard ConvertCellToCycleTask(DataGridViewCellCollection cells)
        {
            CycleTaskCard card = new CycleTaskCard();
            card.CardID = Int64.Parse(cells["ID_Cycle"].Value.ToString());
            card.ExecuteType = cells["ExecuteType_Cycle"].Value.ToString();
            card.Frequency = (FrequencyType)Enum.Parse(typeof(FrequencyType), cells["Frequency_Cycle"].Value.ToString());
            card.Quantity = int.Parse(cells["Quantity_Cycle"].Value.ToString());
            card.Month = UtilityConvert.ParseInt(cells["Month_Cycle"].Value);
            card.Weekday = UtilityConvert.ParseInt(cells["Weekday_Cycle"].Value);
            card.Day = UtilityConvert.ParseInt(Convert.ToString(cells["Day_Cycle"].Value));
            card.Hour = UtilityConvert.ParseInt(Convert.ToString(cells["Hour_Cycle"].Value));
            card.Minute = UtilityConvert.ParseInt(Convert.ToString(cells["Minute_Cycle"].Value));
            card.Context = cells["Context_Cycle"].Value as string;
            card.ExecuteDateTime = DateTime.Parse(cells["NextExecuteTime_Cycle"].Value.ToString());
            card.LastExecuteDateTime = UtilityConvert.ParseDateTime(cells["LastExecuteDateTime_Cycle"].Value);
            card.IfOnlyWorkday = Convert.ToString(cells["IfOnlyWorkDay"].Value).ToLower() != "false"; // Empty and null means true
            return card;
        }

        public static OnceTaskCard ConvertCellToOnceTaskCard(DataGridViewCellCollection cells)
        {
            OnceTaskCard card = new OnceTaskCard();
            card.CardID = Int64.Parse(cells["ID_Once"].Value.ToString());
            card.CreateDateTime = DateTime.Parse(cells["CreateDate_Once"].Value.ToString());
            card.ExecuteType = cells["Type_Once"].Value as string;
            card.Context = cells["Context_Once"].Value as string;
            card.ExecuteDateTime = DateTime.Parse(cells["NextExecuteTime_Once"].Value.ToString());
            card.Note = cells["Note_Once"].Value as string;
            card.LastExecuteDateTime = UtilityConvert.ParseDateTime(cells["LastExecuteDateTime_Once"].Value);
            return card;
        }

        public static string GetDateTimeString(DateTime pDateTime)
        { 
            return pDateTime.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 拆分内容和网址
        /// </summary>
        /// <param name="pContext"></param>
        /// <returns></returns>
        public static ContextAndWebsites SplitContextAndWebsite(string pContext)
        {
            ContextAndWebsites ret = new ContextAndWebsites();
            // 拆分提示文本和网址部分
            string[] tempArry = pContext.Split(new string[] { CommonData.WEBSITE_SPLIT },
                StringSplitOptions.RemoveEmptyEntries);
            ret.Conterxt = tempArry[0];
            // 如果存在网址
            if (tempArry.Length > 1)
            {
                string[] tempArryFroWebsits = tempArry[1].Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                ret.Website = tempArryFroWebsits[0];
                if (tempArryFroWebsits.Length > 1)
                {
                    ret.Website2 = tempArryFroWebsits[1];
                }
            }

            return ret;
        }

        public static string GetCombineWebsiteAddresses(string pWebsite1, string pWebsite2)
        {
            string ret = string.Empty;

            // 如果有网址的话，将会通过分隔符区分。这里区分是为了在展示信息的时候能够显示超链接，方便用户操作。
            if (!string.IsNullOrEmpty(pWebsite1))
            {
                ret = "#Website:" + pWebsite1 + ";" + pWebsite2;
            }

            return ret;
        }
    }
}
